﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace ArkanoidX
{
    public class LevelManager
    {
        public class LevelAttributes
        {
            public int levelNumber;
            public String InputPassword;

            public LevelAttributes(int levelNumber, String inputPassword)
            {
                this.levelNumber = levelNumber;
                this.InputPassword = inputPassword;
            }
        }

        public List<LevelBuilder> _levelBuiders = new List<LevelBuilder>();
        public MainGame _game;

        public MainGame Game
        {
            get { return _game; }
        }

        public Rectangle ArenaArea
        {
            get
            {
                return _game.Arena;
            }
        }

        public LevelManager(MainGame game)
        {
            _game = game;
        }

        public void CreateLevel(int levelNumber, String password, String levelString)
        {
            LevelBuilder levelBuilder
                            = new LevelBuilder(this, levelNumber, password, levelString);
            int index = _levelBuiders.BinarySearch(levelBuilder);
            if (index >= 0)
            {   /* duplicate level */
                // simgply ignore
            }
            else
            {
                index = -index - 1;
                _levelBuiders.Insert(index, levelBuilder);
            }
            password = null;
        }

        public LevelBuilder GetLevelBuilder(int levelNumber)
        {
            LevelBuilder dummy = new LevelBuilder(this, levelNumber, null, null);
            int index = _levelBuiders.BinarySearch(dummy);
            if (index < 0)
            {
                return null;
            }

            return _levelBuiders[index];
        }

        public void LoadLevels(Stream stream)
        {
            StreamReader sr = new StreamReader(stream);
            _levelBuiders.Clear();

            String line;
            String password = null;
            String levelString = "";
            int levelNumber = -1;
            while (!sr.EndOfStream)
            {
                int ip;
                line = sr.ReadLine();
                if ((ip = Utility.CompareString(line, "level")) >= 0)
                {
                    if (levelNumber > 0)
                    {   /* end of last level */
                        CreateLevel(levelNumber, password, levelString);
                    }

                    levelNumber = int.Parse(line.Substring(ip));
                    if (levelNumber <= 0)
                    {   /* invalid level number*/
                        // TODO: ...
                    }
                }
                else if ((ip = Utility.CompareString(line, "password")) >= 0)
                {
                    line = line.Substring(ip);
                    ip = Utility.CompareString(line, "=");
                    if (ip < 0)
                    {   // syntax error
                        // TODO: ...
                    }
                    
                    password = line.Substring(ip).Trim();
                }
                else
                {
                    levelString += line + '\n';
                }
            }
            if (levelNumber > 0)
            {
                CreateLevel(levelNumber, password, levelString);
            }
        }

        public LevelBuilder GetLevelBuilder(LevelAttributes attributes)
        {
            int levelNumber = attributes.levelNumber;
            String userInputPasswd = attributes.InputPassword;

            LevelBuilder levelBuilder = GetLevelBuilder(levelNumber);
            if (levelBuilder == null)
            {
                return null;
            }

            if (!levelBuilder.CheckPassword(userInputPasswd))
            {
                return null;
            }

            return levelBuilder;
        }
    }
}
